package cn.uai.activiti8.dto;

import cn.uai.activiti8.enums.RespCodeEnum;
import com.alibaba.fastjson2.JSON;

import static cn.uai.activiti8.enums.RespCodeEnum.FAIL;
import static cn.uai.activiti8.enums.RespCodeEnum.SUCCESS;

/**
 * @desc
 * @author uai
 * @version 1.0
 * @since 2025年05月27日 23:15
 */
public class Result<T> {

    private Integer code;

    private String message;

    private T data;

    public Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static String success() {
        Result<Object> result = new Result<>(SUCCESS.getCode(), SUCCESS.getMessage(), null);
        return JSON.toJSONString(result);
    }

    public static String success(Object data) {
        Result<Object> result = new Result<>(SUCCESS.getCode(), SUCCESS.getMessage(), data);
        return JSON.toJSONString(result);
    }

    public static String fail(String message) {
        Result<Object> result = new Result<>(FAIL.getCode(), message, null);
        return JSON.toJSONString(result);
    }

    public static String exception(Integer code, String message) {
        Result<Object> result = new Result<>(code, message, null);
        return JSON.toJSONString(result);
    }

    public static String exception(RespCodeEnum code) {
        Result<Object> result = new Result<>(code.getCode(), code.getMessage(), null);
        return JSON.toJSONString(result);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
